<?php
/*********************************************************************************
 * Copyright (C) 2011-2013 X2Engine Inc. All Rights Reserved.
 * 
 * X2Engine Inc.
 * P.O. Box 66752
 * Scotts Valley, California 95067 USA
 * 
 * Company website: http://www.x2engine.com 
 * Community and support website: http://www.x2community.com 
 * 
 * X2Engine Inc. grants you a perpetual, non-exclusive, non-transferable license 
 * to install and use this Software for your internal business purposes.  
 * You shall not modify, distribute, license or sublicense the Software.
 * Title, ownership, and all intellectual property rights in the Software belong 
 * exclusively to X2Engine.
 * 
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EITHER 
 * EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT.
 ********************************************************************************/

/* @edition:pro */

require_once('protected/components/util/CrontabUtil.php');
?>
<h2><?php echo installer_t('Schedule a Cron Task for Automation (optional)'); ?></h2><hr>
	<?php echo installer_t('To trigger scheduled or delayed tasks automatically in X2CRM requires adding a task to the local cron table.');?><br />
	<input type="checkbox" id="startCron" name="startCron" value="1" /> <?php echo installer_t('Create a cron task for X2CRM'); ?>

	<br>
	<div id="start-cron" style="display: none">

		<div id="top-form">
		</div>
		<?php
		$cronRunnerUrl = (empty($_SERVER['HTTPS']) ? 'http' : 'https').'://'.$_SERVER['SERVER_NAME'].str_replace('install.php', '', $_SERVER['REQUEST_URI']).'index.php/api/x2cron';
		$config['cron']['cmd'] = "curl $cronRunnerUrl &>/dev/null"; 
		$config['cron']['tag'] = 'default';
		$config['cron']['desc'] = installer_t('Run delayed or recurring tasks within X2CRM');
		echo CrontabUtil::schedForm($config['cron'], 'cron', $config['cron']['cmd'], $config['cron']['desc']) ;
		?>
	</div>
	<div id="cron-response-box" style="color:red"></div>
	<br>

 <script>
 function testCron(callback){
     $.ajax({
         type: "post",
         url: "initialize.php",
	 data: "testCron=1",
         dataType: 'json'
     }).done(function(r) {
        if(r.error){
		 $('#cron-response-box').html(r.message);
		 $('#startCron').prop('checked',false);
	} else {
		callback();
	}
	 
     });
 }     

$(function(){
	$("#startCron").click( function() {
	     if($("#startCron").is(":checked")) {
	     	testCron(function(){$("#start-cron").slideDown(300);});
	    } else {
	        $("#start-cron").slideUp(300);
	    }
	});
});
 </script>
